<?php

namespace addons\xapply\controller;

use addons\xapply\Base;
use fast\Random;
use app\common\library\Token;
use app\admin\model\xapply\Token as TokenModel;

class Bind extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 绑定推送Token
     * url: http://域名/addons/xapply/bind
     * doc: http://lutui.net/doc/app/bind.html
     */
    public function index()
    {
        $token = $this->auth->getToken();
        Token::delete($token);
        $token = Random::uuid();
        Token::set($token, $this->auth->id, 2592000);
        $tokenInfo = Token::get($token);
        $userPushToken = TokenModel::where('user_id', $this->auth->id)->find();
        if ($userPushToken){
            
        }else{
            $userPushToken = new TokenModel;
            $userPushToken->user_id = $this->auth->id;
        }
        $userPushToken->token = $this->data['pushToken'];
        $userPushToken->lang = $this->data['lang'];
        $userPushToken->device = $this->data['device'];
        $userPushToken->save();
        return json(['error' => '', 'token' => $tokenInfo['token']]);
    }
}
